<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.wlcorp.forcazamis.sys.util.CommonUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="com.wlcorp.forcazamis.domain.pi.Pifitting" %>
<%@ page import="com.wlcorp.forcazamis.domain.pi.Pidetail" %>
<%@ page import="com.wlcorp.forcazamis.sys.util.AppConstant" %>

<%
	Pidetail pidetail = (Pidetail)request.getAttribute("pidetail");
	List pifittingList = (List)request.getAttribute("pifittingList");
%>
<html>
<head>
	<title>销售配件明细</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-Control" content="no-cache"><meta http-equiv="Expires" content="0">
	<link href="${pageContext.request.contextPath}/css/css.css" rel=stylesheet>
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/button_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/form_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/table_css.css" type="text/css">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/css/pagetitle_css.css" type="text/css">
	<script language="javascript" src="${pageContext.request.contextPath}/script/validator.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/script/calendartime.js"></script>
	<script language="javascript" src="${pageContext.request.contextPath}/script/ajax.js"></script>
	<script type="text/javascript">
		function goback(){
			window.location = "${pageContext.request.contextPath}/pi/pidetailManager.do?method=pidetailManager";
		}
		//保存样品子类，包括新增和修改
		function savePifitting(row){
			var piid = document.pidetailForm.piid.value;
			var pifittingId = row.id;
			if("newRow" == pifittingId){
				pifittingId = "";
			}
			var sUrl = "${pageContext.request.contextPath}/pi/pidetailManager.do?method=modifyPifitting&piid=" + piid
				+ "&fittingId=" + pifittingId;
			var params = "";
			var cells = row.cells;
			for(i = 0;i < cells.length;i++){
				var input = cells[i].getElementsByTagName("input");
				if(input.length > 0){
					//先验证必须输入的字段
					if("ename" == input[0].name && input[0].value.length < 1){
						alert("请输入英文描述！");
						return;
					}else if("espec" == input[0].name && input[0].value.length < 1){
						alert("请输入英文规格！");
						return;
					}else if("fittingCount" == input[0].name && input[0].value.length < 1){
						alert("请输入数量！");
						return;
					}else if("unitPrice" == input[0].name && input[0].value.length < 1){
						alert("请输入单价！");
						return;
					}else if("orderNo" == input[0].name && input[0].value.length < 1){
						alert("请输入顺序号！");
						return;
					}
					params = params + "&" + input[0].name + "=" + encodeURI(input[0].value);
				}
			}
			sUrl = sUrl + params;
			ajaxRead(sUrl,function(obj){
				var result = obj.responseText;
				if("" != result){
					row.id = result;
					row.cells[row.cells.length - 1].innerHTML = '<a href="javascript:savePifitting(document.getElementById(' + result + '))">保存</a>'
						+ '&nbsp;<a href="javascript:deletePifitting(document.getElementById(' + result + '))">删除</a>';
					alert("保存成功！");
				}else{
					alert("保存失败！");
				}
			});
		}
		
		function addNewPifitting(){
			var table = document.getElementById("pifittingTbody");
			var rows = table.rows;
			if("newRow" == rows[rows.length - 2].id){
				alert("请先保存前一条样式子类！");
				return;
			}
			appendRow(table);
		}
		
		function appendRow(table){
          var tr = document.createElement('<tr id="newRow" bgcolor="#ffffff">');
          var td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="ename" value="" maxlength="20" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="espec" value="" maxlength="20" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="fittingCount" value="0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="unitPrice" value="0.0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="totalSum" value="0.0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="fittingTag" value="" maxlength="20" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="memo" value="" maxlength="100" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<input class="input" type="text" name="orderNo" value="1" maxlength="10" style="width:80px"/>';
          tr.appendChild(td);
          td = document.createElement('<td>');
          td.innerHTML = '<a href="javascript:savePifitting(document.getElementById(\'newRow\'))">保存</a>';
          tr.appendChild(td);
          table.insertBefore(tr,table.rows[table.rows.length - 1]);
        }

        
        function deletePifitting(tr){
        	if(!confirm("确定删除？")){
        		return;
        	}
        	var fittingId = tr.id;
        	var sUrl = "${pageContext.request.contextPath}/pi/pidetailManager.do?method=deletePifitting&fittingId=" + fittingId
        	ajaxRead(sUrl,function(obj){
        		if("1" == obj.responseText){
        			alert("删除成功！");
        			var table = document.getElementById("pifittingTbody");
        			table.removeChild(tr);
        			
        		}else{
        			alert(obj.responseText);
        		}
        	});
        }
        
        function getTotalSum(tr){
        	var totalPriceTxt = tr.cells[4].getElementsByTagName("input")[0];
        	var priceTxt = tr.cells[3].getElementsByTagName("input")[0];
        	var count = tr.cells[2].getElementsByTagName("input")[0];
        	totalPriceTxt.value = parseFloat(priceTxt.value) * parseFloat(count.value);
        }
        
        function baseInfoEdit(piid){
			window.location = "${pageContext.request.contextPath}/pi/pidetailManager.do?method=getPidetail&piid=" + piid;
		}
		
		function printPiDetail(piid){
			window.open("${pageContext.request.contextPath}/pi/pidetailManager.do?method=printPi&piid=" + piid);
		}
	</script>
</head>
<body>
<CENTER>
<form name="pidetailForm" method="POST" action="${pageContext.request.contextPath}/pidetail/pidetailManager.do?method=modifySample" encType="multipart/form-data">
<input type="hidden" name="piid" value="${pidetail.piid}"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="30">
	<tr> 
	  <td width="22" class="page_titlebar_head">&nbsp;</td>
	  <td class="page_titlebar_bg" width="981">销售配件明细</td>
	</tr>
</table>

<table width="90%" cellspacing="0" cellpadding="0" border="0">
  <tr> 
   	<td align="right"/>
  </tr>
  <tr> 
   	<td align="right"> 
      	<input class="button_common" type="button" name="print" value="PI打印档" style="font-size:9pt" onClick="javascript:printPiDetail('<%=pidetail.getPiid()%>')"> 
    </td>
   </tr>
</table>

<table width="90%" class="InputFrameMain" cellspacing="0" cellpadding="0" border="0" height="100">
  <tr> 
    <td colspan="6" class="table_unfold_titlebar_small_bg">&nbsp;&nbsp;<b>合同基本信息</b></td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="15%">PI编号</td>
		<td width="17%">&nbsp;${pidetail.piCode}</td>
		<td class="table_label" width="15%">客户编号</td>
		<td width="17%">&nbsp;${pidetail.customer.customerName}</td>
		<td class="table_label" width="15%">客户名称</td>
		<td width="17%">&nbsp;${pidetail.customer.customerFullName}</td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="15%">下单日期</td>
		<td width="17%">&nbsp;${pidetail.piDate}</td>
		<td class="table_label" width="15%">合同金额</td>
		<td width="17%">&nbsp;${pidetail.totalSum}</td>
		<td class="table_label" width="15%">合同币种</td>
		<td width="17%">&nbsp;${pidetail.coinTypeName}</td>
  </tr>
  <tr class="InputFrameLine">
  	<td width="100%" colspan="6" align="center"><input type="button" class="button_common_5" name="baseInfoButton" value="基本信息编辑" onclick="javascript:baseInfoEdit('${pidetail.piid}')"/>
  	<td>
  </tr>
  <tr class="InputFrameLine">
        <td class="table_label" width="100%" colspan="6">
        	<table id="" width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#999999">
        	  <tbody id="pifittingTbody">
	          <tr bgcolor="#E7E7E7">
	            <td>英文品名</td>
	            <td>英文规格</td>
	            <td>数量</td>
	            <td>单价</td>
	            <td>金额</td>
	            <td>标签</td>
	            <td>备注</td>
	            <td>顺序号</td>
	            <td>操作</td>
	          </tr>
	          <%
	          	if(null != pifittingList && pifittingList.size() > 0){
	          		for(Iterator iter = pifittingList.iterator();iter.hasNext();){
	          			Pifitting pifitting = (Pifitting)iter.next();
	          			
	          %>
	          <tr id="<%=pifitting.getFittingId()%>" bgcolor="#ffffff">
	            <td><input class="input" type="text" name="ename" value="<%=pifitting.getEname()%>" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="espec" value="<%=pifitting.getEspec()%>" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="fittingCount" value="<%=pifitting.getFittingCount()%>" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="unitPrice" value="<%=pifitting.getUnitPrice()%>" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="totalSum" value="<%=pifitting.getTotalSum()%>" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="fittingTag" value="<%=CommonUtil.nullToStr(pifitting.getFittingTag())%>" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="memo" value="<%=CommonUtil.nullToStr(pifitting.getMemo())%>" maxlength="100" style="width:80px"/></td>
	            <td><input class="input" type="text" name="orderNo" value="<%=pifitting.getOrderNo()%>" maxlength="10" style="width:80px"/></td>	            
	            <td>
	            <%
          			if(null == pidetail.getState() || AppConstant.BILL_STATE_DRAWBACK ==pidetail.getState() || AppConstant.BILL_STATE_SUBMITED == pidetail.getState()){
          	  	%>
	            <a href="javascript:savePifitting(document.getElementById('<%=pifitting.getFittingId()%>'))">保存</a>
	            <a href="javascript:deletePifitting(document.getElementById('<%=pifitting.getFittingId()%>'))">删除</a></td>
	            <%}%>
			  </tr>
			  <%}}%>
			  <tr id="newRow" bgcolor="#ffffff">
	            <td><input class="input" type="text" name="ename" value="" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="espec" value="" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="fittingCount" value="0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="unitPrice" value="0.0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="totalSum" value="0.0" maxlength="10" onkeyup="getTotalSum(this.parentNode.parentNode)" style="width:80px"/></td>
	            <td><input class="input" type="text" name="fittingTag" value="" maxlength="20" style="width:80px"/></td>
	            <td><input class="input" type="text" name="memo" value="" maxlength="100" style="width:80px"/></td>
	            <td><input class="input" type="text" name="orderNo" value="" maxlength="10" style="width:80px"/></td>	            
	            <td><a href="javascript:savePifitting(document.getElementById('newRow'))">保存</a></td>
			  </tr>
			  <%
          		if(null == pidetail.getState() || AppConstant.BILL_STATE_DRAWBACK ==pidetail.getState() || AppConstant.BILL_STATE_SUBMITED == pidetail.getState()){
          	  %>
			  <tr bgcolor="#ffffff">
	            <td colspan="9" align="center"><input type="button" name="addnewPifitting_button" value="新增销售配件" onclick="addNewPifitting()"/></td>
	            
			  </tr>
			  <%}%>
			  </tbody>
		</table>
        </td>
  </tr>
</table>
</form>
</CENTER>

${message}
</body>
</html>
